import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './actions'
import getters from './getters'
Vue.use(Vuex)
// export default new Vuex.Store({
//   state: {
//     cartList: []
//   },
//   mutations: {
//     // addCart (state, payload) {
//     //   const oldProduct = state.cartList.find(item => item.id === payload.id)
//     //   if (oldProduct) {
//     //     oldProduct.count += 1
//     //   } else {
//     //     payload.count = 1
//     //     state.cartList.push(payload)
//     //   }
//     // }
//     addCounter (state, payload) {
//       payload.count++
//     },
//     addToCart (state, payload) {
//       state.cartList.push(payload)
//     }
//   },
//   actions: {
//     addCart (context, payload) {
//       // 1.查找之前数组中是否有该元素
//       const oldProduct = context.state.cartList.find(item => item.id === payload.id)
//       if (oldProduct) {
//         context.commit('addCounter', oldProduct)
//       } else {
//         payload.count = 1
//         context.commit('addToCart', payload)
//       }
//     }
//   }
// })
const state = {
  cartList: JSON.parse(localStorage.getItem('cartList')) || []
}
const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})

export default store
